/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.speech;

import javamail.sip.*;
import java.util.List;
import java.util.ListIterator;
import java.util.StringTokenizer;
import javamail.sip.util.eSipHeader;
import javamail.sip.util.eSipMethodType;
import javamail.sip.util.eSipParseState;
import javamail.sip.util.eSipRespCode;
import javamail.speech.util.eSpeechMsgType;

/**
 *
 * @author sriram
 */
public class speechParser {
    private static final boolean debugEnabled = false;
    String speechText = "START:";
    private speechMessage message = null;

    public speechParser() {
        message = new speechMessage();
    }
    
    public speechParser(List<String> msg) {
        ListIterator list = msg.listIterator();
        if (list.hasNext()) {
            speechText = list.next().toString();
        }
        message = new speechMessage();
    }

    public boolean checkMessage(speechMessage msg) {
        boolean result = false;
        if(message.method != eSpeechMsgType.NIL) {
            if(message.method.hasParms() && message.containsBody()) {
                result = true;
            } else if (!message.method.hasParms()) {
                result = true;
            }
        }
        return result;
    }

    public boolean parse() {
        boolean result = false;
        try {
            message.method = getMethod(speechText);
            message.setBody(getBody(speechText));
            result = checkMessage(message);
        } catch (Exception e) {
            if(debugEnabled)
                System.err.println("speechParser:parse()-Parse error: "+e.getMessage());
        }
        return result;
    }

    private eSpeechMsgType getMethod(String msg) {
        eSpeechMsgType result = eSpeechMsgType.NIL;
        int index = 0;
        if((index = msg.indexOf(":"))>0) {
            //msg = msg.substring(0, index).trim();
            result = result.getFromName(msg.substring(0, index).trim());
        }
        return result;
    }
    private String getBody(String msg) {
        String result = null;
        int index = 0;
        if((index = msg.indexOf(":"))>0) {
            result = msg.substring(index+1).trim();
        }
        return result;
    }
    public speechMessage getMessage() {
        return message;
    }
}
